package com.sunkyc.controller.oss;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.sunkyc.commons.entity.R;
import com.sunkyc.commons.entity.TablePageData;
import com.sunkyc.commons.exception.CustomerException;
import com.sunkyc.commons.entity.PageQueryVo;
import com.sunkyc.module.oss.client.FileService;
import com.sunkyc.module.oss.entity.SysOss;
import com.sunkyc.module.oss.service.SysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 云存储对象 前端控制器
 * </p>
 *
 * @author sunkyc
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/sysOss")
@RequiredArgsConstructor
@Api(tags = "文件对象存储")
public class SysOssController {
    private final SysOssService baseService;

    private final FileService fileService;


    @PostMapping("/upload")
    @ApiOperation("文件上传")
    @SaCheckPermission("oos-file-upload")
    public R<SysOss> fileUpload(@RequestParam("file") MultipartFile multipartFile){
        try {
            SysOss sysOss = fileService.upload(multipartFile);
            sysOss.setCreateBy(StpUtil.getLoginIdAsLong());
            sysOss.setFileOnceName(multipartFile.getOriginalFilename());
            return R.ok(baseService.addFileUpload(sysOss));
        } catch (IOException e) {
            throw new CustomerException("文件上传失败");
        }
    }

    @ApiOperation("获取上传文件大小限制")
    @GetMapping("/uploadMaxSize")
    public R<Long> getUploadSize(){
        return R.ok(baseService.getUploadFileSize());
    }



    @ApiOperation("文件查询[列表]")
    @GetMapping("/list")
    public R<TablePageData<SysOss>> queryFileList(PageQueryVo pageQueryVo,SysOss sysOss){
        return R.ok(baseService.queryFileList(pageQueryVo,sysOss));
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/{ids}")
    public R<Void> delFile(@PathVariable("ids") Long[] ids){
        List<SysOss> sysOssList =baseService.queryListByIds(ids);
        fileService.delete(sysOssList);
        return R.result(baseService.delFile(ids));
    }







}
